<?php
header( 'Location: index.php' ) ;
$table_array=(array)$_POST["t"];
$Field_ID=$_POST["Field_ID"];
$Field_Name=$_POST["Field_Name"];
$Field_Position=$_POST["Field_Position"];
$Field_Length=$_POST["Field_Length"];
$Field_Type=$_POST["Field_Type"];
$Field_Description=$_POST["Field_Description"];
$Field_primary=$_POST["Field_primary"];
$link=mysql_connect('localhost','root','root') or die('fault in connection');
$db="cardescription";
mysql_select_db( $db, $link )or die ( "Couldn't connect $db: ".mysql_error() );
if($_POST['Rec_insert_update']=='insert_table'){
$query01="INSERT INTO `tables` (`name` ,`source` ,`physicalName` ,`contents` ,`length` ,`description`) VALUES (
                     '$table_array[0]',
                     '$table_array[1]',
                     '$table_array[2]',
                     '$table_array[3]',
					 '$table_array[4]',
                     '$table_array[5]')";
mysql_query($query01)or die("Couldn't insert 1 : ".mysql_error());
$table_id_insert=mysql_insert_id();
foreach($Field_Name as $fieldID => $Field_Name_value)
{
  if($Field_primary[$fieldID]){$Field_primary[$fieldID]=1;}else {$Field_primary[$fieldID]=0;}
  $query02="INSERT INTO `fields` (`name` ,`position` ,`length` ,`type` ,`description` ,`tableID`,`isPrimary`) VALUES (
                     '".$Field_Name_value."',
                     '".$Field_Position[$fieldID]."',
                     '".$Field_Length[$fieldID]."',
                     '".$Field_Type[$fieldID]."',
                     '".mysql_real_escape_string($Field_Description[$fieldID])."',
					 '".$table_id_insert."',
                     '".$Field_primary[$fieldID]."')";	
  mysql_query($query02) or die("Couldn't insert2 : ".mysql_error());
  }
}
elseif($_POST['Rec_insert_update']=='update_table'){
$table_id_update=$_POST['table_id'];
$query01="UPDATE tables SET `name`='$table_array[0]' ,
                            `source`='$table_array[1]' ,
			    `physicalName`='$table_array[2]' ,
                            `contents`='$table_array[3]' ,
			    `length`='$table_array[4]' ,
			    `description`='$table_array[5]'
		             WHERE ID ='$table_id_update'; ";
mysql_query($query01) or die("Couldn't update1: ".mysql_error()); 
 foreach($Field_Name as $fieldID => $Field_Nmae_value)
{
if($Field_primary[$fieldID]){$Field_primary[$fieldID]=1;}else {$Field_primary[$fieldID]=0;}
$query02_01= "UPDATE  `fields` SET `name`='".$Field_Nmae_value."' ,
         `position`='".$Field_Position[$fieldID]."' ,
         `length`='".$Field_Length[$fieldID]."',
	 `Type`='".$Field_Type[$fieldID]."',
         `description`='".mysql_real_escape_string($Field_Description[$fieldID])."' ,
	 `isPrimary`='".$Field_primary[$fieldID]."'
	  WHERE  ID='".$Field_ID[$fieldID]."'";
echo "$query02_01";
mysql_query($query02_01) or die("Couldn't update2_1: ".mysql_error());
if(mysql_affected_rows()==0){
     $query02_02="INSERT INTO `fields` (`name` ,`position` ,`length` ,`type` ,`description` ,`tableID`,`isPrimary`) VALUES ('".$Field_Name[$fieldID]."' ,'".$Field_Position[$fieldID]."','".$Field_Length[$fieldID]."','".$Field_Type[$fieldID]."','".mysql_real_escape_string($Field_Description[$fieldID])."','".$table_id_update."','".$Field_primary[$fieldID]."')";
     echo"$query02_02";
     mysql_query($query02_02) or die("Couldn't update2_2 : ".mysql_error());

     }
}
}

?>